<%@page import="com.sun.org.apache.xml.internal.security.utils.resolver.implementations.ResolverDirectHTTP"%>
<%@page import="DB.DBProvider"%>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
<%@ page import="java.util.*, java.io.*,java.sql.ResultSet" %>
<%@ page errorPage="error.jsp" %>
<% 
	boolean requestedUser = (request.getParameter("requestedUser")!= null);
	String user = requestedUser ? request.getParameter("requestedUser")
			:(String)request.getSession().getAttribute("username");
	
    if (user == null) {
		response.sendError(HttpServletResponse.SC_BAD_REQUEST);
		}
    
    Properties p = new Properties();
    p.load(getServletContext().getResourceAsStream("/WEB-INF/config"));
    ResultSet res = (new DBProvider(p)).returnNumberXSLTFile(user);
    res.last();
    int counter = res.getRow();
    int i =3;
    String number =  request.getParameter("number");
%>	

<script type="text/javascript">
	
	function loadXMLDoc(fname) { 
	  var xmlDoc; 
	 
	 // code for IE 
	  if (window.ActiveXObject) { 
	      xmlDoc=new ActiveXObject("Microsoft.XMLDOM"); 
	  } 

	  // code for Mozilla, Firefox, Opera, etc. 
	  else if (document.implementation && document.implementation.createDocument) { 
	      xmlDoc=document.implementation.createDocument("","",null); 
	  } else { 
	       alert('Your browser cannot handle this script'); 
	  } 
	  xmlDoc.async=false; 
	  xmlDoc.load(fname); 
	  return(xmlDoc); 
	} 
	
	function displayResult(index) { 
	    xml=loadXMLDoc("CreateXMLTimeTable?requestedUser=<%=user%>"); 
	    xsl=loadXMLDoc("GetDesign?designNumber="+index); 
	    // code for IE 
	    if (window.ActiveXObject) { 
	        ex=xml.transformNode(xsl);     
	        document.getElementById("timeTable").innerHTML=ex; 
	    } 
	    // code for Mozilla, Firefox, Opera, etc. 
	   else if (document.implementation && document.implementation.createDocument) 
	   { 		
		   	 xsltProcessor=new XSLTProcessor(); 
	         xsltProcessor.importStylesheet(xsl); 
	         resultDocument = xsltProcessor.transformToFragment(xml,document); 
	         $('#timeTable').empty();
	         document.getElementById("timeTable").appendChild(resultDocument);

	    } 
	} 

	$('#BackToUsers').click(function(){
		$('#page').load("/Calendars/Web/users.jsp?number=<%=number%>",function(response, status, xhr) {
			  if (status == "error") {
				    var msg = "Sorry but there was an error: ";
				    $("#page").html("<br />" +"<h1>" + msg + xhr.status + " " + xhr.statusText +"</h1>" );
				  }
				});
	})
		
	$(document).ready(function()
		{	
			$("select").change(function () {
				displayResult($("select option:selected").val());
	        })
	        .change();
		});
	
//	$("#uploadForm").submit(function(event) {  
		/* stop form from submitting normally */    
	//	event.preventDefault();             
		/* get some values from elements on the page: */    
//		var $form = $( this ),        
	//	term = $form.find( 'input[name="filename"]' ).val(),
	//	url = $form.attr( 'action' ); 
	//	alert(url);
	//	alert(term);
		/* Send the data using post and put the results in a div */    
	//	$.post( url, { filename: term },      
	//			function() {alert("adi");});  
	//	});
	</script>
	
	<!-- content -->
	<div id="content" class="post"">
		<div id="timeTable"></div> 
	</div>
	
	<!-- sidebar -->
	<div id="sidebar">
		<ul>
			<li><h2>Files:</h2>
				<center>
					<table>
						<tr><td><a href= "CreateXMLTimeTable?requestedUser=<%=user%>">XML file</a></td></tr>
						<tr><td><a href= "Calendar.dtd">DTD file</a></td></tr>
					</table>
				</center>
			</li>
			<li><h2>Change XSL</h2>
				<center>
					<table>
					<tr><td>
						<select>
							<option value="1"  selected="selected">Design 1</option>
							<option value="2" >Design 2</option>
							<% while (counter > 0){ %>
							<option value="<%=i%>" >Design <%=i%></option>
							<% counter--;
							   i++;
								} %>
						</select>
					</td></tr>
					</table>
				</center>
			</li>
		   <%if (!requestedUser){ %>
			<li><h2>Upload XSL</h2>
				<form method="post" enctype='multipart/form-data' action="UploadFile">
					<table>
						<tr><td><input type="file" name="filename" id="filename"/></td></tr>
						<tr><td><input type="submit" value="Upload" name="submitType"/></td></tr>
					</table>
				</form>
			</li>
			<%} else {%>
			<li><h2>Back to users page</h2>
				<center><button id = "BackToUsers">Click here</button> </center>
			</li> 
			<%}%>
		</ul>
	</div>
